define(["jquery", "easy-admin",'xm-select', "treetable", "iconPickerFa", "autocomplete"], function ($, ea,xmSelect) {

    var table = layui.table,
        treetable = layui.treetable,
        iconPickerFa = layui.iconPickerFa,
        autocomplete = layui.autocomplete;

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'system.fields/index'+parameter,
        add_url: 'system.fields/add'+parameter,
        delete_url: 'system.fields/delete',
        edit_url: 'system.fields/edit'+parameter,
        modify_url: 'system.fields/modify',
    };

    var Controller = {
        index: function () {

            ea.table.render({
                init: init,
                limits:[15,20,30,40,50,60,70,80,90,100,500,1000,5000,10000],
                toolbar:['refresh','add'],limit:CONFIG.ADMINPAGESIZE,
                cols: [[
                   /* {field: 'id', width: 80, title: 'ID',search:false},*/
                    {field: 'sort',  title: '排序', edit: 'text',width:100,search:false},
                    {field: 'name',  title: '字段名称',width:150,search:true},
                    {field: 'xsname', Width: 150, title: '显示名称',search:false, edit: 'text'},
                    {field: 'field',width:200,  title: '字段名'},
                    {field: 'show',width:100, title: '到列表',filter: "show",  search: false, templet: function (data, option){
                            if(data.status.indexOf('noshow')>-1){
                                return '-';
                            }else{
                                return ea.table.switch(data, option);
                            }
                        }},
                    {field: 'edit',width:100, title: '表单', filter: "edit", search: false, templet:function (data, option){
                if(data.status.indexOf("noedit")!==-1){
                    return '-';
                }else{
                    return ea.table.switch(data, option);
                }
            }},
                    {field: 'search',width:100, title: '搜索',  filter: "search",search: false, selectList: {0: '禁用', 1: '启用'}, templet: function (data, option){
                            if(data.status.indexOf("nosearch")!==-1){
                                return '-';
                            }else{
                                return ea.table.switch(data, option);
                            }
                        }},
                    {field: 'export',width:100, title: '导入',  filter: "export",search: false, selectList: {0: '禁用', 1: '启用'}, templet: function (data, option){
                            if(data.status.indexOf("noimport")!==-1){
                                return '-';
                            }else{
                                return ea.table.switch(data, option);
                            }
                        }},
                    {field: 'create_time', minWidth: 200, title: '创建时间', search: 'range'},
                    {
                        width: 150,
                        title: '操作',
                        templet: function (data, option){
                            if(data.issystem===1){
                                return '-';
                            }else{
                                return ea.table.tool(data, option);
                            }
                        },
                        operat: [
                            'edit',
                            'delete'
                        ],
                        fixed:"right"
                    }
                ]],
            });

            // renderTable();

            $('body').on('click', '[data-treetable-refresh]', function () {
                renderTable();
            });

            $('body').on('click', '[data-treetable-delete]', function () {
                var tableId = $(this).attr('data-treetable-delete'),
                    url = $(this).attr('data-url');
                tableId = tableId || init.table_render_id;
                url = url != undefined ? ea.url(url) : window.location.href;
                var checkStatus = table.checkStatus(tableId),
                    data = checkStatus.data;
                if (data.length <= 0) {
                    ea.msg.error('请勾选需要删除的数据');
                    return false;
                }
                var ids = [];
                $.each(data, function (i, v) {
                    ids.push(v.id);
                });
                ea.msg.confirm('确定删除？', function () {
                    ea.request.post({
                        url: url,
                        data: {
                            id: ids
                        },
                    }, function (res) {
                        ea.msg.success(res.msg, function () {
                            renderTable();
                        });
                    });
                });
                return false;
            });

            ea.listen();
        },
        add: function () {
            iconPickerFa.render({
                elem: '#icon',
                url: PATH_CONFIG.iconLess,
                limit: 12,
                click: function (data) {
                    $('#icon').val('fa ' + data.icon);
                },
                success: function (d) {
                    console.log(d);
                }
            });
            autocomplete.render({
                elem: $('#href')[0],
                url: ea.url('system.menu/getMenuTips'),
                template_val: '{{d.node}}',
                template_txt: '{{d.node}} <span class=\'layui-badge layui-bg-gray\'>{{d.title}}</span>',
                onselect: function (resp) {
                }
            });

            ea.listen(function (data) {
                console.log(data);
                return data;
            }, function (res) {
                console.log(res);
                ea.msg.success(res.msg, function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.$('[data-treetable-refresh]').trigger("click");
                });
            });
            this.multiple_select();
        },
        edit: function () {
            iconPickerFa.render({
                elem: '#icon',
                url: PATH_CONFIG.iconLess,
                limit: 12,
                click: function (data) {
                    $('#icon').val('fa ' + data.icon);
                },
                success: function (d) {
                    console.log(d);
                }
            });
            autocomplete.render({
                elem: $('#href')[0],
                url: ea.url('system.menu/getMenuTips'),
                template_val: '{{d.node}}',
                template_txt: '{{d.node}} <span class=\'layui-badge layui-bg-gray\'>{{d.title}}</span>',
                onselect: function (resp) {
                }
            });

            ea.listen(function (data) {
                console.log(data);
                return data;
            }, function (res) {
                console.log(res);
                ea.msg.success(res.msg, function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.$('[data-treetable-refresh]').trigger("click");
                });
            });
            this.multiple_select();
        },

        multiple_select:function (){
            var demo1 = xmSelect.render({
                el: '#multiple-select',
                language: 'zn',
                data: CONFIG.ruleLst,name:'rule'
            })
        }
    };
    return Controller;
});